﻿<Window x:Class="MineSweeper.Windows.Options"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:properties="clr-namespace:MineSweeper.Properties"
        xmlns:valueConverters="clr-namespace:SwiftMVVM.ValueConverters;assembly=SwiftMVVM"
        xmlns:validationRules="clr-namespace:MineSweeper.ValidationRules"
        xmlns:commands="clr-namespace:MineSweeper.Commands"
        xmlns:data="clr-namespace:MineSweeper.Data"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Options" Height="325" Width="370"
        WindowStartupLocation="CenterScreen"
        Name="optionsWindow"
        Style="{StaticResource optionWindowBackgroundStyle}">
    <!--SL 7/18/13 I changed the size of the window because I encountered a problem in which 
    the window didn't accomodate the newly created checkbox-->
    <Window.Resources>
        <valueConverters:EnumEqualsConverter x:Key="EnumEqualsConverter"/>
        <valueConverters:MultiValueConverter x:Key="MultiValueConverter"/>
        <Style TargetType="TextBox">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border  BorderBrush="Black" BorderThickness="1" Background="White" 
                                    CornerRadius="8" Margin="0,0,2,0">
                                <Grid Margin="4">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                    </Grid.RowDefinitions>
                                    <Image Height="16"  Source="/MineSweeper;component/Resources/error.png" Margin="5,2,5,5" VerticalAlignment="Center"/>
                                    <TextBlock DockPanel.Dock="Right" MaxWidth="200" VerticalAlignment="Center"
                                               Grid.Column="1"
                                    TextWrapping="Wrap"
                                    Text="{Binding ElementName=controlWithError, 
                                    TargetNullValue=Invalid,
                                    Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                    </TextBlock>
                                </Grid>
                            </Border>
                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Border BorderBrush="Red" BorderThickness="1">
                                    <AdornedElementPlaceholder Name="controlWithError" />
                                </Border>
                            </Grid>
                        </Grid> 
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" 
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Window.DataContext>
        <ObjectDataProvider ObjectType="{x:Type data:GameOptions}" MethodName="Create"/>
    </Window.DataContext>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <GroupBox Header="Difficulty">
            <Grid>
                <Grid.Resources>
                    <Style TargetType="RadioButton">
                        <Setter Property="Margin" Value="2"/>
                        <Setter Property="GroupName" Value="DifficultyGroup"/>
                    </Style>
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <RadioButton 
                        GroupName="beginner"
                        IsChecked="{Binding Path=Difficulty, 
                        Converter={StaticResource EnumEqualsConverter},
                        ConverterParameter=Beginner,
                        Mode=TwoWay}">
                        <UniformGrid Rows="3" Columns="1">
                            <TextBlock Text="Beginner"/>
                            <TextBlock Text="10 mines"/>
                            <TextBlock Text="9 x 9 tile grid"/>
                        </UniformGrid>
                    </RadioButton>
                    <RadioButton Grid.Row="1"
                        GroupName="intermediate"
                        IsChecked="{Binding Path=Difficulty, 
                        Converter={StaticResource EnumEqualsConverter},
                        ConverterParameter=Intermediate,
                        Mode=TwoWay}">
                        <UniformGrid Rows="3" Columns="1">
                            <TextBlock Text="Intermediate"/>
                            <TextBlock Text="40 mines"/>
                            <TextBlock Text="16 x 16 tile grid"/>
                        </UniformGrid>
                    </RadioButton>
                    <RadioButton Grid.Row="2"
                        GroupName="advanced"
                        IsChecked="{Binding Path=Difficulty, 
                        Converter={StaticResource EnumEqualsConverter},
                        ConverterParameter=Advanced,
                        Mode=TwoWay}">
                        <UniformGrid Rows="3" Columns="1">
                            <TextBlock Text="Advanced"/>
                            <TextBlock Text="99 mines"/>
                            <TextBlock Text="16 x 30 tile grid"/>
                        </UniformGrid>
                    </RadioButton>
                </Grid>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <RadioButton 
                        GroupName="custom"
                        Name="rdoCustomDifficulty" 
                        Content="Custom"  
                        IsChecked="{Binding Path=Difficulty, 
                        Converter={StaticResource EnumEqualsConverter},
                        ConverterParameter=Custom,
                        Mode=TwoWay}"/>       
                    <Grid Margin="5" Grid.Row="1" IsEnabled="{Binding ElementName=rdoCustomDifficulty, Path=IsChecked}">
                        <Grid.Resources>
                            <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=rdoCustomDifficulty, Path=IsChecked}" Value="true">
                                        <Setter Property="IsReadOnly" Value="False"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=rdoCustomDifficulty, Path=IsChecked}" Value="false">
                                        <Setter Property="Text" Value=""/>
                                    </DataTrigger>
                                </Style.Triggers>
                                <Setter Property="Margin" Value="2"/>
                                <Setter Property="Text" Value=""/>
                                <Setter Property="IsReadOnly" Value="True"/>
                            </Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center"/>
                            </Style>
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Text="Height (9-24): "/>
                        <TextBox Grid.Column="1">
                            <TextBox.Text>
                                <Binding Path="CustomHeight" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                                    <Binding.ValidationRules>
                                        <validationRules:IsNumericValidator/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <TextBlock Grid.Row="1" Text="Width (9-30): "/>
                        <TextBox Grid.Column="1" Grid.Row="1">
                            <TextBox.Text>
                                <Binding Path="CustomWidth" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                                    <Binding.ValidationRules>
                                        <validationRules:IsNumericValidator/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <TextBlock Grid.Row="2" Text="Mines (10-668): "/>
                        <TextBox Grid.Column="1" Grid.Row="2">
                            <TextBox.Text>
                                <Binding Path="CustomMineCount" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                                    <Binding.ValidationRules>
                                        <validationRules:IsNumericValidator/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                    </Grid>
                </Grid>
            </Grid>
        </GroupBox>
        <Grid Grid.Row="1" Margin="5">
            <Grid.Resources>
                <Style TargetType="CheckBox">
                    <Setter Property="Margin" Value="4"/>
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <CheckBox Content="Allow Questions Marks" 
                    IsChecked="{Binding Path=AllowCheckMarks,
                    Mode=TwoWay}"/>
            <!--SL 7/18/13 I added this so the user would be able to enable Software Rendering if they encounter a graphics issue. 
            An example of an issue could be that a mine does not fully load-->
            <CheckBox Content="If there are any graphics problems please check this box." 
                    IsChecked="{Binding Path=UseSoftwareRendering,
                    Mode=TwoWay}"  Grid.Row="2"   />
        </Grid>
        <Grid Grid.Row="2" Margin="5">
            <Grid.Resources>
                <Style TargetType="Button">
                    <Setter Property="MinWidth" Value="100" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <Button Grid.Column="1" Content="Ok" IsDefault="True" 
                    Command="{x:Static commands:GameCommands.SaveOptionsAndCloseWindow}">
                <Button.CommandParameter>
                    <MultiBinding Converter="{StaticResource MultiValueConverter}">
                        <MultiBinding.Bindings>
                            <Binding ElementName="optionsWindow"/>
                            <Binding />
                        </MultiBinding.Bindings>
                    </MultiBinding>
                </Button.CommandParameter>
            </Button>
            <Button Grid.Column="2" Content="Cancel" IsCancel="True"/>
        </Grid>
    </Grid>
</Window>
